<template>
  <div class="login-container">
    <div class="login-box">
      <img class="login-logo" src="/img/icons/logon.png" alt="系统logo">
      <div class="login-form">
        <h2 class="title">后台管理系统</h2>
        <p class="subtitle">MANAGEMENT SYSTEM</p>

        <el-form ref="form" :model="form" :rules="rules">
          <el-form-item prop="username">
            <el-input v-model="form.username" placeholder="用户名" prefix-icon="el-icon-user" />
          </el-form-item>

          <el-form-item prop="password">
            <el-input v-model="form.password" placeholder="密码" type="password" prefix-icon="el-icon-lock"
              show-password />
          </el-form-item>

          <el-button type="primary" class="login-btn" :loading="loading" @click="handleLogin">
            登录
          </el-button>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
import http from '@/mixins/http'
export default {
  mixins: [http()],
  data() {
    return {
      form: {
        username: '',
        password: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, message: '密码长度不能小于6位', trigger: 'blur' }
        ]
      },
      loading: false
    }
  },
  methods: {
    async handleLogin() {
      try {
        await this.$refs.form.validate()
        this.loading = true

        const res = await this.$httpPost('/login', this.form)
        localStorage.setItem('token', res.data.token)

        this.$router.push('/list')
        this.$message.success('登录成功')
      } catch (error) {
        this.$handleError(error)
      } finally {
        this.loading = false
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.login-container {
  * {
    margin: 0;
    padding: 0;
  }

  .logon-big {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100vh;
    background-color: #f5f5f5;
  }

  .logon-box {
    width: 1000px;
    height: 700px;
    background-color: white;
  }

  .logon-box img {
    width: 100%;
    height: 700px;
  }

  .center-container {
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    height: 600px;
    width: 300px;
    margin-left: 50px;
  }

  .center-container {
    width: 300px;

    el-input {
      width: 100px;
    }
  }

  .center-container .username-input {
    text-align: left;
    margin-bottom: 20px;
    margin-top: 30px;

    p {
      margin-top: 5px;
      color: red;
    }
  }

  .center-container .password-input {
    text-align: left;
    margin-bottom: 20px;

    // margin-bottom: 40px;
    p {
      color: red;
      margin-top: 5px;
    }
  }</style>
